package com.example.administrator.yuejian35.Find.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian35.Find.activity.PostActivity;
import com.example.administrator.yuejian35.Find.bean.Post;
import com.example.administrator.yuejian35.Home.HomeModel;
import com.example.administrator.yuejian35.Home.bean.Result;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.RoundTransform;
import com.example.administrator.yuejian35.button.ShineButton;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.administrator.yuejian35.Config.CoachImages_URL;
import static com.example.administrator.yuejian35.Config.MasterImages_URL;
import static com.example.administrator.yuejian35.Config.PostImages_URL;

/**
 * Created by HP on 2017/9/26.
 */

public class FindAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Post> postlist=new ArrayList<>();
    private List<Integer> heightList;
    private SharedPreferences settings;
    private SharedPreferences.Editor editor;


    public FindAdapter(Context c, List<Post> postlist){
        this.context = c;
        this.postlist.addAll(postlist);
        heightList = new ArrayList<>();
        for (int i = 0; i < postlist.size(); i++) {
            int height = new Random().nextInt(120) + 300;//[300,420)的随机数
            heightList.add(height);
        }

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //1)  将item.xml 生成 itemView  -  View
        //2)  生成MyViewHolderd对象
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_postlist,parent,false);
        PostViewHolder holder = new PostViewHolder(itemView);
        return holder;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        //用于绑定数据
        //转换类型
        final PostViewHolder viewHolder= (PostViewHolder) holder;

        //由于需要实现瀑布流的效果,所以就需要动态的改变控件的高度了
        ViewGroup.LayoutParams params = PostViewHolder.relativelayout.getLayoutParams();
        params.height=heightList.get(position);
        viewHolder.relativelayout.setLayoutParams(params);

        settings = this.context.getSharedPreferences("User", Context.MODE_PRIVATE);

        editor = settings.edit();

        final Post post = postlist.get(position);

        final String pid = postlist.get(position).getPid();

        viewHolder.name.setText(postlist.get(position).getUname());
        viewHolder.like.setText(postlist.get(position).getHit());

        Picasso.with(context)
                .load(PostImages_URL+postlist.get(position).getPic())
                .into(viewHolder.pic);

        Picasso.with(context)
                .load(CoachImages_URL+postlist.get(position).getUserpic())
                .into(viewHolder.userpic);

        viewHolder.hit.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(View view, boolean checked) {

                Log.v("id",pid);

                if(checked==true){
                    HomeModel homeModel = new HomeModel();
                    Call<Result> call = homeModel.post_hit(pid,"true");
                    call.enqueue(new Callback<Result>() {
                        @Override
                        public void onResponse(Call<Result> call, Response<Result> response) {

                            viewHolder.like.setText(response.body().getResult());
                            Log.v("数量",response.body().getResult());
                            editor.putString("game_hit","true");
                            editor.putString(pid, pid);

                            editor.commit();
                        }

                        @Override
                        public void onFailure(Call<Result> call, Throwable t) {

                        }
                    });
                }
                else if(checked==false){
                    HomeModel homeModel = new HomeModel();
                    Call<Result> call = homeModel.post_hit(pid,"false");
                    call.enqueue(new Callback<Result>() {
                        @Override
                        public void onResponse(Call<Result> call, Response<Result> response) {

                            viewHolder.like.setText(response.body().getResult());
                            Log.v("数量f",response.body().getResult());
                            editor.putString("game_hit","false");
                            editor.putString(pid, pid);
                            editor.commit();
                        }

                        @Override
                        public void onFailure(Call<Result> call, Throwable t) {

                        }
                    });
                }
            }
        });

        String bbb = settings.getString("game_hit","");
        String id = settings.getString(pid,"-1");
        Log.v("bbb",bbb);
        Log.v("ssid",id);
        if(bbb.equals("true") && id.equals(pid)){
           viewHolder.hit.setChecked(true);
        }



        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,PostActivity.class);
                intent.putExtra("post", post);
                context.startActivity(intent);
            }
        });



    }



    @Override
    public int getItemCount() {
        return postlist.size();
    }
}

